layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    //Banner列表
    var tableIns = table.render({
        elem: '#banner_list',
        url : '/listBanner',
        cellMinWidth : 95,
        page : false,
        height : "full",
        id : "banner_list_table",
        cols : [[
            {field: 'businessId', title: '商品编号', width:150, align:"center"},
            {field: 'title', title: '标题',align:"center"},
            {field: 'displayOrder', title: '排序', align:'center',width:100},
            {field: 'statusStr', title: '状态',  align:'center',width:150},
            {field: 'picUrl', title: '图片URL', align:'center'},
            {title: '操作', width:170, templet:'#banner_list_bar',align:"center"}
        ]]
    });

    //添加Banner
    function addBanner(data){
        var index = layui.layer.open({
            title : "添加Banner",
            type : 2,
            content : "bannerAdd.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(data){
                    body.find(".businessId").val(data.businessId);
                    body.find(".title").val(data.title);
                    body.find(".displayOrder").val(data.displayOrder);
                    body.find(".status").attr("data-value",data.status);

                    body.find("input[name='picUrl']").val(data.picUrl);
                    body.find("input[name='bannerId']").val(data.id);

                    body.find(".thumbImg").attr("src",data.picUrl);
                    form.render();
                    form.render("select");
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回Banner列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addBanner_btn").click(function(){
        addBanner();
    });

    //列表操作
    table.on('tool(banner_list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addBanner(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此Banner？',{icon:3, title:'提示信息'},function(index){
                $.get("/deleteBannerById",{
                    id : data.id
                },function(data){
                    tableIns.reload();
                    layer.close(index);
                })
            });
        }
    });
})